import React, { Component } from "react";
import { Platform, Image } from "react-native";
import { StackActions, NavigationActions } from "react-navigation";
import NavBarConfig from "../modules/NavBar";
import { getLoginState } from "../../util/loginStateManager";

class LaunchScreen extends Component {
  static navigationOptions = ({ navigation }) =>
    NavBarConfig(
      {
        headerTitle: "",
        header: null
      },
      navigation
    );
  componentWillMount() {
    if (Platform.OS == "ios") {
      if (getLoginState()) {
        this.loginSuccessHandle("Home");
      } else {
        this.loginSuccessHandle("Login");
      }
    }
  }

  componentDidMount() {
    if (Platform.OS !== "ios") {
      if (getLoginState()) {
        setTimeout(() => {
          this.loginSuccessHandle("Home");
        }, Math.round(Math.random() * 800) + 500);
      } else {
        setTimeout(() => {
          this.loginSuccessHandle("Login");
        }, Math.round(Math.random() * 800) + 500);
      }
    }
  }

  loginSuccessHandle(page = "Login") {
    const resetAction = StackActions.reset({
      index: 0,
      actions: [NavigationActions.navigate({ routeName: page })]
    });
    this.props.navigation.dispatch(resetAction);
  }

  render() {
    return (
      <Image
        style={{
          resizeMode: "stretch",
          position: "absolute",
          width: "100%",
          height: "100%"
        }}
        source={require("../../assets/launchImage.png")}
      />
    );
  }
}

export default LaunchScreen;
